package com.filebackup.struts.actions;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.filebackup.business.UserService;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.UserVo;

public class UserSelectAction extends FilebackupSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserService userService;

	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		JSONArray proArr = getFolder();

		response.setContentType("text/html;charset=UTF-8");
		System.out.println("3333");
		PrintWriter out = response.getWriter();
		System.out.println("2222");
		out.print(proArr);
		out.flush();
		out.close();
		return null;
	}

	public JSONArray getFolder() {
		JSONArray objArr = new JSONArray();
		List<Vo> voList = this.getUserService().findAll();
		for (int i = 0; i < voList.size(); i++) {
			if (this.getSessionUser().getId().intValue() != ((UserVo) voList.get(i))
					.getId().intValue()) {
				System.out.println("in");
				JSONObject obj = new JSONObject();
				obj.put("id", ((UserVo) voList.get(i)).getId());
				obj.put("name", ((UserVo) voList.get(i)).getName());
				objArr.add(obj);
			}
		}
		return objArr;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

}
